2020-2021 Sunseeker Telemetry and Lighting System
ctsd16_ex4_internalTempSensor.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430FG662x Demo - CTSD16, Using the Integrated Temperature Sensor, Ext. Res.
34 //
35 // Description: This program uses the CTSD16 module to perform a single
36 // conversion on a single channel which is internally connected to the CTSD16's
37 // temperature sensor and an external resistor feeding the CTSD16 clock. Once
38 // the conversion is completed, the result is stored in a variable then converted
39 // into Celsius and Fahrenheit values.
40 //
41 // Test by setting a breakpoint at the indicated line. Upon reaching the breakpoint
42 // the conversion result will be stored in the results array. The result will
43 // then be taken and converted into degrees K, C, and F and be saved in the same
44 // array.
45 //
46 // ACLK = 32kHz, MCLK = SMCLK = Calibrated DCO = 16.384MHz, SD_CLK = 1.024MHz
47 // * Ensure low_level_init.c is included when building/running this example *
48 //
49 // Notes: For minimum Vcc required for CTSD16 module - see datasheet
50 // 1nF cap btw Vref and AVss is recommended when using 1.2V ref
51 //
52 // Sensor's temperature coefficient is 2.158mV/K (from datasheet)
53 // Sensor's offset voltage ranges from -100mv to 100mV (assume 0)
54 // Vsensor = 1.32mV * DegK + Vsensor_offset (assuming 0mv for offset)
55 // Vsensor = (CTSD16MEM0)/32767 * Vref(mV)
56 // DegK = (CTSD16MEM0 * 1200)/32767/2.158 = (CTSD16MEM0 * 1200)/70711
57 // DegC = DegK - 273
58 // DegF = (DegC * 9/5) + 32
59 //
60 //
61 // MSP430FG662x
62 // -----------------
63 // /|\| |
64 // | | |
65 // --|RST |
66 // | | (A0.6+/- connected internally)
67 // |A0.6+ VREF |---+ (to CTSD16's temperature sensor)
68 // |A0.6- | |
69 // | | -+- 1nF
70 // | | -+-
71 // | | |
72 // | AVss |---+
73 //
83 //******************************************************************************
84 #include "inc/hw_memmap.h"
85 #include "wdt_a.h"
86 #include "gpio.h"
87 #include "ctsd16.h"
88 
89 uint32_t results[4]; // CTSD16 Conversion and Temp Results
90  // results[0] = raw CTSD16 results
91  // results[1] = temp in K
92  // results[2] = temp in C
93  // results[3] = temp in F
94 
95 void main(void) {
96  // Stop WDT
97  WDT_A_hold(WDT_A_BASE);
98 
99  // Select AD0+/- analog input pins
100  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P6, GPIO_PIN4 | GPIO_PIN5);
101 
102  // Initialize CTSD16 using internal reference and internal resistor for clock
103  CTSD16_init(CTSD16_BASE,
104  CTSD16_RTR_INPUT_CHARGEPUMP_BURST_REQUEST_DISABLE, CTSD16_REF_INTERNAL);
105 
106  // Initialize converter 0: internal temperature sensor channel 6
107  CTSD16_initConverter(CTSD16_BASE, CTSD16_CONVERTER_0, CTSD16_SINGLE_MODE,
108  CTSD16_INPUT_CH6);
109 
110  // Clear converter 0 interrupt flag
111  CTSD16_clearInterrupt(CTSD16_BASE, CTSD16_CONVERTER_0,
112  CTSD16_CONVERTER_INTERRUPT);
113  // Enable result interrupt
114  CTSD16_enableInterrupt(CTSD16_BASE, CTSD16_CONVERTER_0,
115  CTSD16_CONVERTER_INTERRUPT);
116 
117  // Delay ~120us for 1.2V ref to settle
118  __delay_cycles(2000);
119 
120  while(1) {
121  __no_operation(); // SET BREAKPOINT HERE
122  // Set bit to start conversion
123  CTSD16_startConverterConversion(CTSD16_BASE, CTSD16_CONVERTER_0);
124  __bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts
125  __no_operation(); // For debugger
126 
127  // Calculate Temperatures in different scales
128  results[1] = ((unsigned long)results[0] * 1200)/70711;
129  results[2] = results[1] - 273;
130  results[3] = (results[2] * 9/5) + 32;
131 
132  __no_operation(); // SET BREAKPOINT HERE
133  }
134 }
135 
136 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
137 #pragma vector=CTSD16_VECTOR
138 __interrupt
139 #elif defined(__GNUC__)
140 __attribute__((interrupt(CTSD16_VECTOR)))
141 #endif
142 void CTSD16_ISR(void) {
143  switch (__even_in_range(CTSD16IV,CTSD16IV_CTSD16MEM0)) {
144  case CTSD16IV_NONE: break;
145  case CTSD16IV_CTSD16OVIFG: break;
146  case CTSD16IV_CTSD16MEM0:
147  // Save CH0 results (clears IFG)
148  results[0] = CTSD16_getResults(CTSD16_BASE, CTSD16_CONVERTER_0);
149  break;
150  default: break;
151  }
152  __bic_SR_register_on_exit(LPM0_bits); // Wake up from LPM0
153 }
154 
void main(void)
uint32_t results[4]
void CTSD16_ISR(void)
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)